<?php
namespace app\common\service;
use library\tools\Http;

class LatLngTranslate
{
    /**
     * 地址转换经纬度
     *
     * @Author       flowerHua <875501286@qq.com>
     * @Copyright    广州拓冠科技 <http://my8m.com>
     * @Time         2020-04-01
     * @param        $address string 地址(广东省广州市天河区xxx号)
     * @return       array [经度,维度]
     */
    public static function Address2LatLng($address) {
        $headers[] = 'Referer: http://lbs.qq.com/webservice_v1/guide-suggestion.html';
        $headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36';
        $url = "http://apis.map.qq.com/ws/place/v1/suggestion/?®ion=&key=EY4BZ-JCOEX-FSS4H-TRBUK-ILN4Q-HKFP4&keyword=" . $address;
        $data = Http::get($url, [], ['headers' => $headers]);
        $data = json_decode($data, true);
        $res = [];
        if ($data['status'] === 0) {
            $res = $data['data'][0]['location'];
        }
        return $res;
    }
    /**
     * 经纬度转换地址
     *
     * @Author       flowerHua <875501286@qq.com>
     * @Copyright    广州拓冠科技 <http://my8m.com>
     * @Time         2020-04-01
     * @param        float $lat 经度
     * @param        float $lng 纬度
     * @return       string 地址
     */
    public static function LatLng2Address($lat=0.0,$lng=0.0) {
        $headers[] = 'Referer: http://lbs.qq.com/webservice_v1/guide-suggestion.html';
        $headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36';
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?location={$lat},{$lng}&key=EY4BZ-JCOEX-FSS4H-TRBUK-ILN4Q-HKFP4";
        $data = Http::get($url, [], ['headers' => $headers]);
        $data = json_decode($data, true);
        $res = '';
        if ($data['status'] === 0) {
            $res = $data['result']['address'];
        }
        return $res;
    }

    /**
     * 获取两个经纬度之间的距离
     *
     * @Author       flowerHua <875501286@qq.com>
     * @Copyright    广州拓冠科技 <http://my8m.com>
     * @Time         2020-04-01
     * @param        $lat1   string  第1个位置经度
     * @param        $lng1   string  第1个位置维度
     * @param        $lat2   string  第2个位置经度
     * @param        $lng2   string  第2个位置纬度
     * @param        $len_type int 计算方式(1:km 2:m)
     * @param        $decimal  int 保留位数
     * @return       float     1.11
     */
    public static function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2) {
        define('EARTH_RADIUS', 6378.137);
        $radLat1 = $lat1 * PI() / 180.0;   //PI()圆周率
        $radLat2 = $lat2 * PI() / 180.0;
        $a = $radLat1 - $radLat2;
        $b = ($lng1 * PI() / 180.0) - ($lng2 * PI() / 180.0);
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $s = $s * EARTH_RADIUS;
        $s = round($s * 1000);
        if ($len_type == 1) {
            $s /= 1000;
        }
        return round($s, $decimal);
    }
     
    // public static function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2) {
    //     define('EARTH_RADIUS', 6378.137);
    //     $radLat1 = $lat1 * PI() / 180.0;   //PI()圆周率
    //     $radLat2 = $lat2 * PI() / 180.0;
    //     $a = $radLat1 - $radLat2;
    //     $b = ($lng1 * PI() / 180.0) - ($lng2 * PI() / 180.0);
    //     $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
    //     $s = $s * EARTH_RADIUS;
    //     $s = round($s * 1000);
    //     if ($len_type == 1) {
    //         $s /= 1000;
    //     }
    //     return round($s, $decimal);
    // }
    
    
    /**
     * 通过经纬度，获得附近范围内
     * @param  $latitude    纬度    
     * @param  $longitude    经度
     * @param  $raidus        半径范围(单位：米)
     * @return multitype:number
     */
    public function getAround($latitude,$longitude,$raidus){
        $PI = 3.14159265;
        $degree = (24901*1609)/360.0;
        $dpmLat = 1/$degree;
        $radiusLat = $dpmLat*$raidus;
        $minLat = $latitude - $radiusLat;
        $maxLat = $latitude + $radiusLat;
        $mpdLng = $degree*cos($latitude * ($PI/180));
        $dpmLng = 1 / $mpdLng;
        $radiusLng = $dpmLng*$raidus;
        $minLng = $longitude - $radiusLng;
        $maxLng = $longitude + $radiusLng;
        return ( array (minLat=>$minLat, maxLat=>$maxLat, minLng=>$minLng, maxLng=>$maxLng));
    }
    
    
}